namespace BTSPracCOurse.BAM.Sample {
    
    
    [Microsoft.XLANGs.BaseTypes.SchemaReference(@"BTSPracCOurse.BAM.Sample.PurchaseOrderSchema", typeof(BTSPracCOurse.BAM.Sample.PurchaseOrderSchema))]
    [Microsoft.XLANGs.BaseTypes.SchemaReference(@"BTSPracCOurse.BAM.Sample.InvoiceSchema", typeof(BTSPracCOurse.BAM.Sample.InvoiceSchema))]
    public sealed class Transform_POToInvoice : Microsoft.XLANGs.BaseTypes.TransformBase {
        
        private const string _strMap = @"<?xml version=""1.0"" encoding=""UTF-16""?>
<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" xmlns:var=""http://schemas.microsoft.com/BizTalk/2003/var"" exclude-result-prefixes=""msxsl var userCSharp"" version=""1.0"" xmlns:ns0=""http:/www.MoustafaRefaat.com/BTSPracCourse"" xmlns:userCSharp=""http://schemas.microsoft.com/BizTalk/2003/userCSharp"">
  <xsl:output omit-xml-declaration=""yes"" method=""xml"" version=""1.0"" />
  <xsl:template match=""/"">
    <xsl:apply-templates select=""/ns0:PurchaseOrder"" />
  </xsl:template>
  <xsl:template match=""/ns0:PurchaseOrder"">
    <xsl:variable name=""var:v1"" select=""userCSharp:MathMultiply(string(Header/@PurchaseOrderID) , &quot;100&quot;)"" />
    <xsl:variable name=""var:v2"" select=""userCSharp:DateCurrentDateTime()"" />
    <xsl:variable name=""var:v3"" select=""userCSharp:StringTrimRight(&quot;Client&quot;)"" />
    <ns0:Invoice>
      <Header>
        <PurchaseOrderInfo>
          <xsl:if test=""Header/@PurchaseOrderID"">
            <xsl:attribute name=""ID"">
              <xsl:value-of select=""Header/@PurchaseOrderID"" />
            </xsl:attribute>
          </xsl:if>
          <xsl:if test=""Header/@PODate"">
            <xsl:attribute name=""Date"">
              <xsl:value-of select=""Header/@PODate"" />
            </xsl:attribute>
          </xsl:if>
          <xsl:value-of select=""Header/text()"" />
        </PurchaseOrderInfo>
        <InvoiceInfo>
          <xsl:attribute name=""ID"">
            <xsl:value-of select=""$var:v1"" />
          </xsl:attribute>
          <xsl:attribute name=""Date"">
            <xsl:value-of select=""$var:v2"" />
          </xsl:attribute>
          <xsl:if test=""Header/@ClientID"">
            <xsl:attribute name=""ClientID"">
              <xsl:value-of select=""Header/@ClientID"" />
            </xsl:attribute>
          </xsl:if>
          <xsl:attribute name=""ClientName"">
            <xsl:value-of select=""$var:v3"" />
          </xsl:attribute>
          <xsl:variable name=""var:v4"" select=""userCSharp:InitCumulativeSum(0)"" />
          <xsl:for-each select=""/ns0:PurchaseOrder/LineItem"">
            <xsl:variable name=""var:v5"" select=""userCSharp:MathMultiply(string(@Price) , &quot;1.0&quot;)"" />
            <xsl:variable name=""var:v6"" select=""userCSharp:MathMultiply(string(@Quantity) , &quot;0.15&quot; , string($var:v5))"" />
            <xsl:variable name=""var:v7"" select=""string(@Quantity)"" />
            <xsl:variable name=""var:v8"" select=""userCSharp:MathMultiply($var:v7 , string($var:v5))"" />
            <xsl:variable name=""var:v9"" select=""userCSharp:MathAdd(string($var:v6) , string($var:v8))"" />
            <xsl:variable name=""var:v10"" select=""userCSharp:AddToCumulativeSum(0,string($var:v9),string(./text()))"" />
          </xsl:for-each>
          <xsl:variable name=""var:v11"" select=""userCSharp:GetCumulativeSum(0)"" />
          <xsl:attribute name=""Total"">
            <xsl:value-of select=""$var:v11"" />
          </xsl:attribute>
        </InvoiceInfo>
      </Header>
      <Items>
        <xsl:for-each select=""LineItem"">
          <xsl:variable name=""var:v12"" select=""userCSharp:StringTrimRight(&quot;Item&quot;)"" />
          <xsl:variable name=""var:v13"" select=""string(@Price)"" />
          <xsl:variable name=""var:v14"" select=""userCSharp:MathMultiply($var:v13 , &quot;1.0&quot;)"" />
          <xsl:variable name=""var:v15"" select=""string(@Quantity)"" />
          <xsl:variable name=""var:v16"" select=""userCSharp:MathMultiply($var:v15 , &quot;0.15&quot; , string($var:v14))"" />
          <xsl:variable name=""var:v17"" select=""userCSharp:MathMultiply($var:v15 , string($var:v14))"" />
          <xsl:variable name=""var:v18"" select=""userCSharp:MathAdd(string($var:v16) , string($var:v17))"" />
          <Item>
            <xsl:if test=""@ID"">
              <xsl:attribute name=""ItemID"">
                <xsl:value-of select=""@ID"" />
              </xsl:attribute>
            </xsl:if>
            <xsl:attribute name=""Name"">
              <xsl:value-of select=""$var:v12"" />
            </xsl:attribute>
            <xsl:if test=""@Price"">
              <xsl:attribute name=""UnitPrice"">
                <xsl:value-of select=""@Price"" />
              </xsl:attribute>
            </xsl:if>
            <xsl:if test=""@Quantity"">
              <xsl:attribute name=""Quantity"">
                <xsl:value-of select=""@Quantity"" />
              </xsl:attribute>
            </xsl:if>
            <xsl:attribute name=""Discount"">
              <xsl:value-of select=""$var:v14"" />
            </xsl:attribute>
            <xsl:attribute name=""Tax"">
              <xsl:value-of select=""$var:v16"" />
            </xsl:attribute>
            <xsl:attribute name=""Total"">
              <xsl:value-of select=""$var:v18"" />
            </xsl:attribute>
            <xsl:value-of select=""./text()"" />
          </Item>
        </xsl:for-each>
      </Items>
      <xsl:value-of select=""./text()"" />
    </ns0:Invoice>
  </xsl:template>
  <msxsl:script language=""C#"" implements-prefix=""userCSharp""><![CDATA[
public string MathMultiply(string param0, string param1)
{
	System.Collections.ArrayList listValues = new System.Collections.ArrayList();
	listValues.Add(param0);
	listValues.Add(param1);
	double ret = 1;
	bool first = true;
	foreach (string obj in listValues)
	{
		double d = 0;
		if (IsNumeric(obj, ref d))
		{
			if (first)
			{
				first = false;
				ret = d;
			}
			else
			{
				ret *= d;
			}
		}
		else
		{
			return """";
		}
	}
	return ret.ToString(System.Globalization.CultureInfo.InvariantCulture);
}


public string MathMultiply(string param0, string param1, string param2)
{
	System.Collections.ArrayList listValues = new System.Collections.ArrayList();
	listValues.Add(param0);
	listValues.Add(param1);
	listValues.Add(param2);
	double ret = 1;
	bool first = true;
	foreach (string obj in listValues)
	{
		double d = 0;
		if (IsNumeric(obj, ref d))
		{
			if (first)
			{
				first = false;
				ret = d;
			}
			else
			{
				ret *= d;
			}
		}
		else
		{
			return """";
		}
	}
	return ret.ToString(System.Globalization.CultureInfo.InvariantCulture);
}


public string MathAdd(string param0, string param1)
{
	System.Collections.ArrayList listValues = new System.Collections.ArrayList();
	listValues.Add(param0);
	listValues.Add(param1);
	double ret = 0;
	foreach (string obj in listValues)
	{
	double d = 0;
		if (IsNumeric(obj, ref d))
		{
			ret += d;
		}
		else
		{
			return """";
		}
	}
	return ret.ToString(System.Globalization.CultureInfo.InvariantCulture);
}


public string DateCurrentDateTime()
{
	DateTime dt = DateTime.Now;
	string curdate = dt.ToString(""yyyy-MM-dd"", System.Globalization.CultureInfo.InvariantCulture);
	string curtime = dt.ToString(""T"", System.Globalization.CultureInfo.InvariantCulture);
	string retval = curdate + ""T"" + curtime;
	return retval;
}


public string StringTrimRight(string str)
{
	if (str == null)
	{
		return """";
	}
	return str.TrimEnd(null);
}


public string InitCumulativeSum(int index)
{
	if (index >= 0)
	{
		if (index >= myCumulativeSumArray.Count)
		{
			int i = myCumulativeSumArray.Count;
			for (; i<=index; i++)
			{
				myCumulativeSumArray.Add("""");
			}
		}
		else
		{
			myCumulativeSumArray[index] = """";
		}
	}
	return """";
}

public System.Collections.ArrayList myCumulativeSumArray = new System.Collections.ArrayList();

public string AddToCumulativeSum(int index, string val, string notused)
{
	if (index < 0 || index >= myCumulativeSumArray.Count)
	{
		return """";
    }
	double d = 0;
	if (IsNumeric(val, ref d))
	{
		if (myCumulativeSumArray[index] == """")
		{
			myCumulativeSumArray[index] = d;
		}
		else
		{
			myCumulativeSumArray[index] = (double)(myCumulativeSumArray[index]) + d;
		}
	}
	return (myCumulativeSumArray[index] is double) ? ((double)myCumulativeSumArray[index]).ToString(System.Globalization.CultureInfo.InvariantCulture) : """";
}

public string GetCumulativeSum(int index)
{
	if (index < 0 || index >= myCumulativeSumArray.Count)
	{
		return """";
	}
	return (myCumulativeSumArray[index] is double) ? ((double)myCumulativeSumArray[index]).ToString(System.Globalization.CultureInfo.InvariantCulture) : """";
}

public bool IsNumeric(string val)
{
	if (val == null)
	{
		return false;
	}
	double d = 0;
	return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d);
}

public bool IsNumeric(string val, ref double d)
{
	if (val == null)
	{
		return false;
	}
	return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d);
}


]]></msxsl:script>
</xsl:stylesheet>";
        
        private const string _strArgList = @"<ExtensionObjects />";
        
        private const string _strSrcSchemasList0 = @"BTSPracCOurse.BAM.Sample.PurchaseOrderSchema";
        
        private const string _strTrgSchemasList0 = @"BTSPracCOurse.BAM.Sample.InvoiceSchema";
        
        public override string XmlContent {
            get {
                return _strMap;
            }
        }
        
        public override string XsltArgumentListContent {
            get {
                return _strArgList;
            }
        }
        
        public override string[] SourceSchemas {
            get {
                string[] _SrcSchemas = new string [1];
                _SrcSchemas[0] = @"BTSPracCOurse.BAM.Sample.PurchaseOrderSchema";
                return _SrcSchemas;
            }
        }
        
        public override string[] TargetSchemas {
            get {
                string[] _TrgSchemas = new string [1];
                _TrgSchemas[0] = @"BTSPracCOurse.BAM.Sample.InvoiceSchema";
                return _TrgSchemas;
            }
        }
    }
}
